package routes

import (
	"manage/internal/routes/middleware/handler"
	"manage/internal/routes/model"
	"manage/internal/service"

	"github.com/gin-gonic/gin"
	khttp "github.com/go-kratos/kratos/v2/transport/http"

	_ "manage/docs" // 导入生成的docs包

	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// RegisterHttpServer 注册 Gin 服务器
func RegisterHttpServer(s *khttp.Server, srv *service.GatewayService) {
	router := gin.New()
	InitConfig(router)
	// common.TaskRuner(common.CpuTask)
	// 添加Swagger路由
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

	rootGrp := router.Group("/api/v1")
	{
		// 登录注册
		{
			rootGrp.POST("/login", handler.Login(srv))
			rootGrp.POST("/register", handler.Register(srv))
		}
		// 用户组
		userGrp := rootGrp.Group("/user")
		{
			userGrp.Use(handler.ProxyAuth(srv, model.Admin, model.Teacher, model.Student)) // [管理员|老师]
			userGrp.POST("/", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Post)
			userGrp.GET("/user_list", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Get)
			userGrp.GET("/:user_id", srv.Remote.User().Get)
			userGrp.PUT("/:user_id", srv.Remote.User().Put)
			userGrp.DELETE("/:user_id", handler.ProxyAuth(srv, model.Admin), srv.Remote.User().Delete)
		}
		// 考试组
		examGrp := rootGrp.Group("/exam")
		{
			examGrp.POST("/", handler.ProxyAuth(srv, model.Admin, model.Teacher), srv.Remote.Exam().Post)
			examGrp.GET("/:exam_id", srv.Remote.Exam().Get)
			examGrp.DELETE("/:exam_id", handler.ProxyAuth(srv, model.Admin, model.Teacher), srv.Remote.Exam().Delete)
			examGrp.PUT("/:exam_id", handler.ProxyAuth(srv, model.Admin, model.Teacher), srv.Remote.Exam().Put)
		}
		// 课程组 [管理员]
		courseGrp := rootGrp.Group("/course")
		{
			courseGrp.Use(handler.ProxyAuth(srv, model.Admin)) // [管理员]
			courseGrp.POST("/", srv.Remote.Course().Post)
			courseGrp.GET("/:course_id", srv.Remote.Course().Get)
			courseGrp.DELETE("/:course_id", srv.Remote.Course().Delete)
			courseGrp.PUT("/:course_id", srv.Remote.Course().Put)
		}
		// 班级组 [管理员]
		classGrp := rootGrp.Group("/class")
		{
			classGrp.POST("/", handler.ProxyAuth(srv, model.Admin), srv.Remote.Class().Post)
			classGrp.GET("/:class_id", handler.ProxyAuth(srv, model.Admin), srv.Remote.Class().Get)
			classGrp.DELETE("/:class_id", handler.ProxyAuth(srv, model.Admin), srv.Remote.Class().Delete)
			classGrp.PUT("/:class_id", handler.ProxyAuth(srv, model.Admin), srv.Remote.Class().Put)
			// 学生班级 [老师 | 管理员]
			classJLGrp := classGrp.Group("/:class_id")
			{
				classJLGrp.Use(handler.ProxyAuth(
					srv,
					model.Admin, model.Teacher,
				))
				classJLGrp.POST("/join", srv.Remote.Student().Post)
				classJLGrp.DELETE("/leave", srv.Remote.Student().Delete)
			}
		}
		// 服务状态
		// rootGrp.GET("/ws", middleware.MonitorWS)
	}
	s.HandlePrefix("/", router)
}
